package engine.newUi
{
	import com.greensock.TimelineLite;
	import com.greensock.TweenLite;
	
	import engine.logic.AlertLogic;
	
	import flash.display.Sprite;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.DropShadowFilter;
	import flash.text.TextFormat;
	
	import newx.NxSprite;
	import newx.com.control.text.NxLabel;
	
	public class MarqueeUI extends NxSprite
	{
		private static var _self	:MarqueeUI;
		private var _isInit:Boolean	=	false;
		/**字幕数组*/
		private var infoArray		:Array		=	new Array();
		private var urgentArray	:Array		=	new Array();
		private var generaArray	:Array		=	new Array();
		
		/**单个字幕显示的次数,默认为1次*/
		private var showNumber		:uint		=	2;
		private var showNowNum		:uint		=	0;
		
		/**当前显示信息类型*/
		private var showTyep		:String;
		
		/**字幕的滚动速度与移动像素*/
		private var showSpeed		:Number		=	2;
		
		/**字幕的宽高度*/
		private var _width			:uint		=	0;
		private var _height		:uint		=	0;
		
		/**文字的偏移量*/
		private var _left			:int		=	4;
		private var _top			:int		=	4;
		private var _txtWidth		:uint	=	0;
		private var _txtHeight		:uint	=	0;
		
		/**可视区域的左边距和上边距*/
		
		/**皮肤背景名*/
		private var _skinName		:String		=	"";
		
		/**当前是否有字幕在滚动*/
		private var movieStar		:Boolean	=	false;
		
		/**显示容器*/
//		private var backSpace		:Sprite;
		private var textSpace		:Sprite;
		private var textFile		:NxLabel;
//		private var backMc			:MovieClip;
		
		/**当前显示对象的宽度*/
		private var showTextW		:Number;
		private var showtextH		:Number;
		private var moveX			:Number	=	0;
		
		//
		private var _timeline		:TimelineLite;
		
		private var _txtRealWidth	:uint;
		
		private var _currentTxt	:String	=	"";
		
		private var _alertLogic    :AlertLogic;
		
		/**
		 * 当完成初始化工作之后调用
		 */
		public static function getInstance():MarqueeUI
		{
			if(_self == null)
			{
				_self	=	new MarqueeUI();
			}
			return _self
		}
		
		public function Init(width:uint = 200,height:uint=50,skinName:String="",tl:uint=0,tp:uint=0,txtW:uint=200,txtH:uint=30):void
		{
//			visible	=	false;
			if(!_isInit)
			{
				_left		=	tl;
				_top		=	tp;
				_txtWidth	=	txtW;
				_txtHeight	=	txtH;
				_width		=	width;
				_height		=	height;
				_skinName	=	skinName;
				textFile	=	new NxLabel();
				textSpace	=	new Sprite();
				
				var textFormat : TextFormat = new TextFormat();
				textFormat.size = 14;
				textFormat.bold = true;
				textFile.setTextFormat( textFormat );
//				backSpace	=	new Sprite();
				
				//设定文本的滤镜效果
				var filter:BitmapFilter 		= 	getBitmapFilter();
				var myFilters:Array 			= 	new Array();
				myFilters.push(filter);
				textFile.filters				=	myFilters;
				
//				addChild(backSpace);
				//绘制字幕背景
				drawBack(_width,_height);
				_timeline = new TimelineLite({onComplete:tweenFinish});
			}
			_isInit	=	true;
			textFile.mouseEnabled	=	false;
			textFile.mouseChildren	=	false;
			this.mouseEnabled	=	false;
			this.mouseChildren	=	false;
			
			_alertLogic = AlertLogic.getInstance();
		}
		
		public function MarqueeUI()
		{
		}
		/**
		 * 
		 * @param _w
		 * @param _h
		 * 根据设定宽高来绘制背景
		 * 当皮肤参数为空时，自动绘制一个透明度为30%的单色背景 
		 */		
		private function drawBack(_w:uint,_h:uint):void
		{
//			if(_skinName == "")
//			{
//				var tempMc:Sprite	=	new Sprite();
//				tempMc.graphics.beginFill(0xFF8800,0.3);
//				tempMc.graphics.drawRect(0,0,_w,_h);
//				tempMc.graphics.endFill();
//				backSpace.addChild(tempMc);
//			}
//			else
//			{
//				backMc			=	ResManager.newResClass(_skinName) as MovieClip;
//				backMc.x		=	0;
//				backMc.y		=	0;
//				backMc.width	=	_w;
//				backMc.height	=	_h;
//				backSpace.addChild(backMc);
//			}
			
			var txtMask:Sprite	=	new Sprite();
			txtMask.graphics.beginFill(0,1);
			txtMask.graphics.drawRect(0,0,_txtWidth,_txtHeight);
			txtMask.graphics.endFill();
			addChild(txtMask);
			
			var textContainer:Sprite	=	new Sprite();
			
			txtMask.x	=	textContainer.x	=	_left;
			txtMask.y	=	textContainer.y	=	_top;
			addChild(textContainer);
			textContainer.addChild(textSpace);
			textContainer.mask	=	txtMask;
			
		}
		/**
		 * 添加要显示的字幕
		 */
		private function addInfo(val:String,type:String):void
		{
			_timeline.kill();
			_timeline = new TimelineLite({onComplete:tweenFinish});
			textSpace.x = 0;			
//			show(MainTopIndexUI.getInstance().marqueeBg)
			mouseChildren	=	mouseEnabled	=	false;
			drawTextFiled(val,"Urgent");
			movieStar	=	true;
			_alertLogic.showing = true;
			//开始移动文本
			marMove();
		}
		/**
		 * 绘制要显示的内容
		 */		
		private function drawTextFiled(str:String,type:String):void
		{
			var showText:String				=	 "<font color='#FF8800'>"+str+"</font>";
			textFile.htmlText				=	showText;
			
			textSpace.addChild(textFile);
			if(textSpace.width<_txtWidth)
			{
				textSpace.x	=	(_txtWidth-textSpace.width)/2
			}

		}
		//滤镜效果
		private function getBitmapFilter():BitmapFilter 
		{
			var color:Number 		= 	0x000000;
			var angle:Number 		= 	45;
			var alpha:Number 		= 	1;
			var blurX:Number 		= 	1;
			var blurY:Number 		= 	1;
			var distance:Number 	= 	1;
			var strength:Number 	= 	0.8;
			var inner:Boolean 		= 	false;
			var knockout:Boolean 	= 	false;
			var quality:Number 		= 	BitmapFilterQuality.HIGH;
			return new DropShadowFilter(distance,
				angle,
				color,
				alpha,
				blurX,
				blurY,
				strength,
				quality,
				inner,
				knockout);
		}
		
		/**
		 *添加计时器 
		 */		
		private function marMove():void
		{
			textSpace.y	=	_txtHeight;
			_timeline.append(new TweenLite(textSpace,1,{alpha:1, y:0}));
			if(_txtWidth<textSpace.width)
			{
				_timeline.append(new TweenLite(textSpace,2,{x:_txtWidth-textSpace.width}));
			}
			
			if(_txtHeight<textSpace.height)
			{
				var distance:int	=	_txtHeight-textFile.height;
				_timeline.append(new TweenLite(textSpace,Math.abs(distance)/10,{y:distance}));
			}
			
			textSpace.y	=	0;
			_timeline.append(new TweenLite(textSpace,2,{}));
			_timeline.append(new TweenLite(textSpace,2,{alpha:0, y:-_txtHeight}));
			_timeline.play();
		}
		/**
		 *删除字幕 
		 */		
		public function tweenFinish():void
		{
			_timeline.clear();
			hide();
//			visible	=	false;
			_alertLogic.showing = false;
			_alertLogic.ShowMsg();
			textSpace.x = 0;	
			//_timeline.clear();
		}
		/**
		 *添加普通消息 
		 */		
		public static function generalNews(str:String):void
		{
			if(str	== ""||str==null)
			{
				return;
			}
			else
			{
				_self.addInfo(str,"Genera")
			}
		}
		/**
		 *发布紧急消息 
		 */		
		public static function urgentNews(str:String):void
		{
			if(str	== ""||str==null)
			{
				return;
			}
			else
			{
				_self.addInfo(str,"Urgent")
			}
		}
		
		public static function setXY(x:uint,y:uint):void
		{
			_self.x	=	x;
			_self.y	=	y;
		}
	}
}